import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { StorehouseService } from 'src/app/@core/services/storehouse/storehouse.service';
import { WorkShopService } from 'src/app/@core/services/workshop/work-shop.service';
import { EditWorkShopComponent } from '../../basic-data/work-shop/edit-work-shop/edit-work-shop.component';

@Component({
  selector: 'app-inventory-jilu',
  templateUrl: './inventory-jilu.component.html',
  styleUrls: ['./inventory-jilu.component.scss']
})
export class InventoryJiluComponent {
  value1 = [];
  tableData:any[]=[]
  allCheckedStatus = false;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  checkedIds = [];
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any; }[];
  config = {
    id: 'Add-role',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  filterValue:string = '';
  TreeData:any = [];
   obj = {
    Part: '',
    Type: '',
    StartTime: '',
    EndTime: '',
    PageIndex: 1,
    PageSize: 20
  };
  showLoading = false;
  view = {
    top: '45%',
    left: '50%'
  };
  type = [
    '入库','出库'
  ]
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  state: any;
  constructor(private dialogService: DialogService,private SharedMethodService:SharedMethodService,private WorkShopService:WorkShopService,private Router:Router,private StorehouseService:StorehouseService){}
  ngOnInit(): void {
    this.showLoading = true;
    this.StorehouseService.GetInventoryDetailsPaged(this.obj).subscribe((data:any)=>{
      this.tableData = data.items
      this.showLoading = false;
      this.pager.total = data.totalCount
    })
  }
  dataTableOptions = {
    columns: [
      {
        field: '单据编码',
        header: '单据编码',
        fieldType: 'text',
      },
      {
        field: '单据类型',
        header: '单据类型',
        fieldType: 'text',
      },

      {
        field: '物料编码',
        header: '物料编码',
        fieldType: 'text'
      },
      {
        field: '物料名称',
        header: '物料名称',
        fieldType: 'text'
      },
      {
        field: '物料规格',
        header: '物料规格',
        fieldType: 'text'
      },
      {
        field: '单位',
        header: '单位',
        fieldType: 'text'
      },

      {
        field: '仓库',
        header: '仓库',
        fieldType: 'text'
      },
      {
        field: '库位',
        header: '库位',
        fieldType: 'text'
      },
      {
        field: '类型',
        header: '类型',
        fieldType: 'text'
      },
      {
        field: '出/入库数量',
        header: '出/入库数量',
        fieldType: 'text'
      },

      {
        field: '出/入库时间',
        header: '出/入库时间',
        fieldType: 'text'
      },
      {
        field: '执行人',
        header: '执行人',
        fieldType: 'text'
      },
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field:'单据编码',
      width:'160px'
    },
    {
      field:'单据类型',
      width:'150px'
    },
    {
      field:'物料编码',
      width:'150px'
    },
    {
      field:'物料名称',
      width:'150px'
    },
    {
      field:'单位',
      width:'150px'
    },

    {
      field:'仓库',
      width:'150px'
    },
    {
      field:'库位',
      width:'150px'
    },
    {
      field:'类型',
      width:'150px'
    },
    {
      field:'出/入库数量',
      width:'150px'
    },
    {
      field:'创建人',
      width:'150px'
    },
    {
      field:'创建时间',
      width:'160px'
    },
    {
      field:'创建时间',
      width:'160px'
    },
  ];//表格宽度配置
  pageIndexChange($event: any) {
   this.filter()
  }//切换页码
  pageSizeChange($event: any){
    this.filter()
  }//改变每页显示数量
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  EditRole(rowItem:any,dialogtype?:string){
    let data = {
      rowItem:rowItem,
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data:data,
      title: '更新车间',
      content: EditWorkShopComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.workShop.workshopName){
              this.WorkShopService.UpdateWorkshop(results.modalContentInstance.workShop,rowItem.workshopId).subscribe((data:any)=>{
                results.modalInstance.hide();
                this.showToast('success','成功','车间更新成功')
                this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tableData=data.items;
                  this.pager.total=data.totalCount;
                })
              },(error:any)=>{
                results.modalInstance.hide();
                this.showToast('error','失败',error.error)
              })
            }else{
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//更新车间
  filter(){
    this.showLoading = true;
    this.StorehouseService.GetInventoryDetailsPaged(this.obj).subscribe((data:any)=>{
      this.tableData = data.items
      this.showLoading = false;
      this.pager.total = data.totalCount
    })
  }//搜索
  filter2(){
    this.obj.PageIndex = 1;
    this.showLoading = true;
    this.StorehouseService.GetInventoryDetailsPaged(this.obj).subscribe((data:any)=>{
      this.tableData = data.items
      this.showLoading = false;
      this.pager.total = data.totalCount
    })
  }//搜索
  reset(){
    this.obj = {
      Part: '',
      Type: '',
      StartTime: '',
      EndTime: '',
      PageIndex: 1,
      PageSize: 20
    };
    this.value1 = [];
    this.filter()
  }//重置
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)

  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable,'workshopId').subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }//获取选中行
  cleared(){
    this.SharedMethodService.cleared(this.checkedIds,this.tableData,this.datatable).subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }//清空选中行
  start$end(){
    this.obj.StartTime = this.value1[0].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/');
    this.obj.EndTime = this.value1[1].toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/');
  }
}
